package com.foreveross.action;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.SYWebService.SYHttpclientCall;
import com.foreveross.SYWebService.vo.ConditionVo;
import com.foreveross.SYWebService.vo.DataVo;
import com.foreveross.SYWebService.vo.SYResult;
import com.foreveross.SYWebService.vo.SYSWin;
import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.common.Exception.SYHttpclientCallException;
import com.foreveross.service.ExpressService;
import com.foreveross.service.HouseUserService;
import com.foreveross.service.LogService;
import com.foreveross.util.AuthUtil;
import com.foreveross.util.ObjectToMap;
import com.foreveross.vo.Express;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.HrNewsInfo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/sysHouseUserController")
public class SysHouseUserController extends BaseCostroller {

	private Logger log = Logger.getLogger(SysHouseUserController.class);

	@Resource
	private HouseUserService houseUserService;

	@Resource
	private LogService logService;

	/**
	 * 业主信息查询
	 * 
	 * @param request
	 * @param response
	 * @param name
	 * @param telephone
	 * @param type
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHouseUser")
	public void getHouseUser(HttpServletRequest request,
			HttpServletResponse response, String name, String houseId,
			String telephone, String authenticationStatus, String type,
			String rows, String page,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			int countRecord = (int) houseUserService.getHouseUserListCount(
					name, houseId, telephone, authenticationStatus, type,userId);
			PageVo<HouseUser> pageObj = new PageVo<HouseUser>(page,
					countRecord, rows);
			pageObj.setRows(houseUserService.findHouseUserList(name, houseId,
					telephone, authenticationStatus, type,
					pageObj.getpageIndex(), pageObj.getpageSize(),userId));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("业主信息 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("业主信息  查看失败");
			log.error("业主信息 查看失败!", e);
		}
		List listz = Arrays.asList("messageHouses","newsHouses","optimizedHouses");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo, listz);
	}

	/**
	 * 查看个人信息
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findHouseUser")
	public void findHouseUser(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			HouseUser hrHouses = (HouseUser) this.findObject(HouseUser.class,
					id);
			resultVo.setSuccess(true);
			resultVo.setData(hrHouses);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看个人信息失败");
		}
		List listz = Arrays.asList("messageHouses","newsHouses","optimizedHouses");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,listz);
	}

	/**
	 * 认证确定
	 * 
	 * @param request
	 * @param response
	 * @param houseUser
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/authentication")
	public void authentication(HttpServletRequest request,
			HttpServletResponse response, HouseUser houseUser) {
		ResultVo resultVo = new ResultVo();
		try {
			houseUserService.authentication(houseUser);
			resultVo.setSuccess(true);
			String isState = houseUser.getAuthenticationStatus() == 1 ? "通过"
					: "不通过";
			log.info("业主信息认证" + isState);
			logService
					.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,
							houseUser.getAuthenticationStatus() == 1 ? Constants.LOG_AUTHENTICATION
									: Constants.LOG_UNAUTHENTICATION, "业主信息认证"
									+ isState, AuthUtil.getUserid(request),
							"业主信息" + houseUser.getId() + "认证" + isState);
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("业主信息认证 失败");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 联网认证
	 * @param request
	 * @param response
	 * @param userName
	 * @param telephone
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/userOwnerVerify")
	public void userOwnerVerify(HttpServletRequest request,
			HttpServletResponse response, String userName, String telephone,
			String projectId) {
		//后台不再需要进行认证，屏蔽掉此操作
//		ResultVo resultVo = new ResultVo();
//		try{
//			ConditionVo conditionVo = new ConditionVo();
//			conditionVo.setP0("UserRev_OwnerVerify");
//			conditionVo.setP1(userName);
//			conditionVo.setP2(telephone);
//			conditionVo.setP3(projectId);
//			DataVo dataVo = houseUserService.userOwnerVerify(conditionVo);
//			resultVo.setSuccess(true);
//			resultVo.setData(dataVo);
//			
//		}catch(SYHttpclientCallException e){
//			resultVo.setSuccess(false);
//			resultVo.setMessage(e.getMessage());
//		}catch(Exception e){
//			resultVo.setSuccess(false);
//			resultVo.setMessage("联网认证失败");
//		}
//		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 业主信息添加修改
	 * 
	 * @param request
	 * @param response
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateHouseUser")
	public void addOrUpdateHouseUser(HttpServletRequest request,
			HttpServletResponse response, HouseUser houseUser,
			String updateType, String houseId) {
		ResultVo resultVo = new ResultVo();
		try {
			houseUser.setHrHouses(new HrHouses(houseId));
			HrHouses houses = (HrHouses) this.findObject(HrHouses.class,
					houseId);

			houseUser.setAddress(houses.getProvince().getCodeDesc() + houses.getName()
					+ houseUser.getBuildingCode());

			houseUserService.saveOrUpdate(houseUser, updateType);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,
					updateType.equals(Constants.UPDATE_ADD) ? Constants.LOG_ADD
							: Constants.LOG_UPDATE,
					"业主信息" + Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request),
					Constants.getUpdateType(updateType) + "业主信息["
							+ houseUser.toString() + "]");
			log.info("业主信息 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("业主信息 " + updateType + "失败");
			log.error("业主信息" + updateType + "失败!", e);
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 业主信息删除
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteHouseUserList")
	public void deleteHouseUserList(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			houseUserService.deleteHouseUserList(ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_DEL, "业主信息删除",
					AuthUtil.getUserid(request), "业主信息[" + ids + "]");
			log.info("业主信息 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage(Constants.SYS_BASEDATA_DEL_ERRE);
			log.info("业主信息 删除失败!");
			e.printStackTrace();
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	public static void main(String[] args) {
		ResultVo resultVo = new ResultVo();
		try{
			ConditionVo conditionVo = new ConditionVo();
			conditionVo.setP0("UserRev_OwnerVerify");
			conditionVo.setP1("艾阳");
			conditionVo.setP2("13800010006");
			conditionVo.setP3("新天地大厦");
			
			DataVo datavo = null;
			Map<String, String> params = new HashMap<String, String>();
			params = ObjectToMap.objectToMapViaFields(conditionVo, true);
			SYResult sysResult = SYHttpclientCall.getSYData(params);
			List<SYSWin> list = sysResult.getUserRev_GetOwnerInfor();
			if (list != null && !list.isEmpty()) {
				SYSWin sYSWin = list.get(0);
				List<DataVo> listv = sYSWin.getSyswin();
				if (listv != null && !listv.isEmpty()) {
					datavo = listv.get(0);
				}
			}
			
			resultVo.setSuccess(true);
			resultVo.setData(datavo);
			
		}catch(SYHttpclientCallException e){
			resultVo.setSuccess(false);
			resultVo.setMessage(e.getMessage());
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("联网认证失败");
		}
		
		System.out.println(resultVo.isSuccess());
		System.out.println(resultVo.getMessage());
	}

}
